### Library

library('SDMTools')

### Base dir

base.dir = '/home/31/jc165798/Climate/AWAP.direct.download/'

### Enter points to intersect data with

pnts = data.frame(east=153.1235,north=-28.2227)

### Need to extract rain, tmin, and tmax

for (a in c('solar')) # Could also be tmax, tmin or rain

	{
	
	### New directory
	
	a.dir = paste(base.dir,a,'/',sep="")
	
	### Extract daily and monthly
	
	for (res in c('daily','monthly'))
	
		{
	
		b.dir = paste(a.dir,res,'/',sep='')
		
		###  List all files in b.dir
		
		tfiles = list.files(b.dir, full.names=T, pattern='.asc.gz')
		
		### Position tracker
		
		outdata = pnts
		
		### Position tracker
		
		i=1
		
		### Loop through individual files
		
			for (tfile in tfiles)
			
			{
		
			### Read in a file
		
			tasc = read.asc.gz(paste(tfile,sep=''))
			
			### Get data of interest
			
			tdata = extract.data(pnts,tasc)
			
			### Get a name
			
			
			nm = substr(tfile,nchar(tfile)-14,nchar(tfile)-7)
			
			### Append data
			
			outdata = cbind(outdata,tdata)
			
			### Change a name
			
			names(outdata)[i+2] = paste(nm,sep='')
			
			### Change position tracker
			
			i=i+1
			
			### Report progress
			
			cat('\n',tfile,'\n')
			
			}
		
		write.csv(outdata,paste('/home/99/jc152199/AWAPforLuke/',res,'_',a,'_AWAP_point_extract.csv',sep=''), row.names=F)	
		
		}
		
	}
	
### Close loop
		
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			